package cn.cdtu.tibetplan.service.impl;

import java.util.List;
import cn.cdtu.tibetplan.mapper.SchoolMapper;
import cn.cdtu.tibetplan.pojo.PageResult;
import cn.cdtu.tibetplan.pojo.School;
import cn.cdtu.tibetplan.pojo.SchoolExample;
import cn.cdtu.tibetplan.pojo.SchoolWithBLOBs;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.cdtu.tibetplan.pojo.SchoolExample.Criteria;
import cn.cdtu.tibetplan.service.SchoolService;
import org.springframework.stereotype.Service;

/**
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class SchoolServiceImpl implements SchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    /**
     * 查询全部
     */
    @Override
    public List<School> findAll() {
        return schoolMapper.selectByExample(null);
    }

    public SchoolWithBLOBs show(Long id){
        return schoolMapper.selectByPrimaryKey(id.intValue());
    }

    /**
     * 按分页查询
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<School> page = (Page<School>) schoolMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加
     */
    @Override
    public void add(SchoolWithBLOBs school) {
        schoolMapper.insert(school);
    }

    /**
     * 修改
     */
    @Override
    public void update(SchoolWithBLOBs school) {
        schoolMapper.updateByPrimaryKeySelective(school);
    }

    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */
    @Override
    public School findOne(Long id) {
        return schoolMapper.selectByPrimaryKey(id.intValue());
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            schoolMapper.deleteByPrimaryKey(id.intValue());
        }
    }

    @Override
    public PageResult findPage(SchoolWithBLOBs school, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        SchoolExample example = new SchoolExample();
        Criteria criteria = example.createCriteria();
        if (school != null) {
            if (school.getSchoolname() != null && school.getSchoolname().length() > 0) {
                criteria.andSchoolnameLike("%" + school.getSchoolname() + "%");
            }
            if (school.getIntro() != null && school.getIntro().length() > 0) {
                criteria.andIntroLike("%" + school.getIntro() + "%");
            }
            if (school.getAddress() != null && school.getAddress().length() > 0) {
                criteria.andAddressLike("%" + school.getAddress() + "%");
            }
            if (school.getIshelp() != null && school.getIshelp().length() > 0) {
                criteria.andIshelpLike("%" + school.getIshelp() + "%");
            }
            if (school.getIsneed() != null && school.getIsneed().length() > 0) {
                criteria.andIsneedLike("%" + school.getIsneed() + "%");
            }
            if (school.getHisroty() != null && school.getHisroty().length() > 0) {
                criteria.andHisrotyLike("%" + school.getHisroty() + "%");
            }
            if (school.getUpdatetime() != null && school.getUpdatetime().length() > 0) {
                criteria.andUpdatetimeLike("%" + school.getUpdatetime() + "%");
            }
            if (school.getOperator() != null && school.getOperator().length() > 0) {
                criteria.andOperatorLike("%" + school.getOperator() + "%");
            }
            if (school.getOther1() != null && school.getOther1().length() > 0) {
                criteria.andOther1Like("%" + school.getOther1() + "%");
            }
            if (school.getOther2() != null && school.getOther2().length() > 0) {
                criteria.andOther2Like("%" + school.getOther2() + "%");
            }
            if (school.getOther3() != null && school.getOther3().length() > 0) {
                criteria.andOther3Like("%" + school.getOther3() + "%");
            }
        }
        Page<SchoolWithBLOBs> page = (Page<SchoolWithBLOBs>) schoolMapper.selectByExampleWithBLOBs(example);
        return new PageResult(page.getTotal(), page.getResult());
    }

}
